package com.qingyun.schedule.controller;

import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.qingyun.common.dto.Result;
import com.qingyun.schedule.client.StudentClient;
import com.qingyun.schedule.entity.Course;
import com.qingyun.schedule.entity.Schedule;
import com.qingyun.schedule.entity.Student;
import com.qingyun.schedule.mapper.CourseMapper;
import com.qingyun.schedule.service.IScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 教师课表安排 前端控制器
 * </p>
 *
 * @author your_name
 * @since 2025-08-23
 */
@RestController
public class ScheduleController {
    
    @Autowired
    private IScheduleService scheduleService;

    @Autowired
    private StudentClient studentClient;
    
    @Autowired
    private CourseMapper courseMapper;



    @GetMapping("/querySchedule")
    public Result querySchedule(
            @RequestParam("teacherId") Long teacherId, 
            @RequestParam("date") String dateStr,
            @RequestHeader("Authorization") String token) {
        
        // 参数验证
        if (teacherId == null) {
            return Result.fail("教师ID不能为空");
        }
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return Result.fail("查询日期不能为空");
        }
        
        return scheduleService.querySchedule(teacherId, dateStr);
    }

//    @GetMapping("/queryScheduleToFeedback")
//    public Result queryScheduleToFeedback(@RequestParam("teacherId") Long teacherId,
//                                          @RequestParam("date") String dateStr,
//                                          @RequestHeader("Authorization") String token){
//        // 参数验证
//        if (teacherId == null) {
//            return Result.fail("教师ID不能为空");
//        }
//        if (dateStr == null || dateStr.trim().isEmpty()) {
//            return Result.fail("查询日期不能为空");
//        }
//
//        return scheduleService.queryScheduleToFeedback(teacherId, dateStr);
//    }
    @GetMapping("/queryStudentSchedule")
    public Result queryScheduleByStudent(
            @RequestParam(value = "teacherId", required = false) Long teacherId,
            @RequestParam(value = "date", required = false) String dateStr,
            @RequestParam(value = "studentId") Long studentId,
            @RequestHeader("Authorization") String token) {

        // 参数验证
        if(studentId==null){
            return Result.fail("学生ID不能为空");
        }
        return scheduleService.queryStudentSchedule(teacherId, dateStr, studentId);
    }

    @GetMapping("/queryStudentScheduleByName")
    public Result queryScheduleByStudentName(
            @RequestParam("studentName") String studentName,
            @RequestParam(value = "teacherId", required = false) Long teacherId,
            @RequestParam(value = "date", required = false) String dateStr) {
        if (studentName == null || studentName.trim().isEmpty()) {
            return Result.fail("学生姓名不能为空");
        }
        Student student = studentClient.getStudentByName(studentName);
        if (student == null) {
            return Result.fail("未找到该学生");
        }
        return scheduleService.queryStudentSchedule(teacherId, dateStr, student.getId());
    }
    @GetMapping("/internal/byId")
    public Schedule byId(@RequestParam("scheduleId")Long scheduleId){
        return scheduleService.getBaseMapper().selectById(scheduleId);
    }
    
    /**
     * 基于课表ID获取课表详细信息（供其他服务调用）
     */
    @GetMapping("/internal/scheduleInfo")
    public Result getScheduleInfo(@RequestParam("scheduleId") Long scheduleId) {
        try {
            Schedule schedule = scheduleService.getById(scheduleId);
            if (schedule == null) {
                return Result.fail("课表不存在");
            }
            
            com.qingyun.schedule.dto.ScheduleInfoDTO scheduleInfo = new com.qingyun.schedule.dto.ScheduleInfoDTO();
            scheduleInfo.setId(schedule.getId());
            scheduleInfo.setTeachDate(schedule.getTeachDate());
            scheduleInfo.setStartTime(schedule.getStartTime());
            scheduleInfo.setEndTime(schedule.getEndTime());
            scheduleInfo.setPlace(schedule.getPlace());
            
            // 获取课程名称
            if (schedule.getCourseId() != null) {
                Result courseResult = getCourseName(schedule.getCourseId());
                if (BooleanUtil.isTrue(courseResult.getSuccess())) {
                    scheduleInfo.setCourseName((String) courseResult.getData());
                }
            }
            
            return Result.ok(scheduleInfo);
        } catch (Exception e) {
            return Result.fail("获取课表信息失败");
        }
    }
    
    /**
     * 基于课程ID获取课程信息（供其他服务调用）
     */
    @GetMapping("/internal/courseInfo")
    public Result getCourseName(@RequestParam("courseId") Long courseId) {
        try {
            Course course = courseMapper.selectById(courseId);
            if (course == null) {
                return Result.fail("课程不存在");
            }
            return Result.ok(course.getName());
        } catch (Exception e) {
            return Result.fail("获取课程信息失败");
        }
    }
}

